package com.whq.thrift.api.web.admin.db.verify;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class MerchantApiPermissionSqlProvider {
	public String getSelectMerchantApiPermissionsSql(Map<String, Object> map) {
//		SqlBuilder.BEGIN();
//		SqlBuilder.SELECT("*");
//		SqlBuilder.FROM("t_merchant_api_permission");
//		String merchantName = (String)map.get("merchantName");
//		String apiMethod = (String)map.get("apiMethod");
//		if (StringUtil.isNotBlank(merchantName)){
//			SqlBuilder.WHERE("merchant_name like " + merchantName);
//		}
//		if (StringUtil.isNotBlank(apiMethod)){
//			SqlBuilder.WHERE("api_method like " + apiMethod);
//		}
//		SqlBuilder.ORDER_BY("id asc");
		
		StringBuilder sb = new StringBuilder();
		sb.append("select * from t_merchant_api_permission");
		String merchantName = (String)map.get("merchantName");
		String apiMethod = (String)map.get("apiMethod");
		if (StringUtil.isNotBlank(merchantName) || StringUtil.isNotBlank(apiMethod)){
			sb.append(" where ");
		}
		if (StringUtil.isNotBlank(merchantName)){
			sb.append("merchant_name like '"+ merchantName+"'");
		}
		if (StringUtil.isNotBlank(merchantName) && StringUtil.isNotBlank(apiMethod)){
			sb.append(" and ");
		}
		if (StringUtil.isNotBlank(apiMethod)){
			sb.append("api_method like '"+ apiMethod+"'");
		}
		sb.append(" order by id asc");
		System.out.println(sb.toString());
		return sb.toString() + " limit #{beginIndex}, #{maxCount} ";
	}
	
	public String getSelectCountSql(Map<String, Object> map){
//		SqlBuilder.BEGIN();
//		SqlBuilder.SELECT("count(*)");
//		String merchantName = (String)map.get("merchantName");
//		String apiMethod = (String)map.get("apiMethod");
//		if (StringUtil.isNotBlank(merchantName)){
//			SqlBuilder.WHERE("merchant_name like " + merchantName);
//		}
//		if (StringUtil.isNotBlank(apiMethod)){
//			SqlBuilder.WHERE("api_method like " + apiMethod);
//		}
//		SqlBuilder.FROM("t_merchant_api_permission");
		
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*) from t_merchant_api_permission");
		String merchantName = (String)map.get("merchantName");
		String apiMethod = (String)map.get("apiMethod");
		if (StringUtil.isNotBlank(merchantName) || StringUtil.isNotBlank(apiMethod)){
			sb.append(" where ");
		}
		if (StringUtil.isNotBlank(merchantName)){
			sb.append("merchant_name like '"+ merchantName+"'");
		}
		if (StringUtil.isNotBlank(merchantName) && StringUtil.isNotBlank(apiMethod)){
			sb.append(" and ");
		}
		if (StringUtil.isNotBlank(apiMethod)){
			sb.append("api_method like '"+ apiMethod+"'");
		}
		return sb.toString();
	}
	
	
	public String getUpdateSql(MerchantApiPermission merchantApiPermission){
		SqlBuilder.BEGIN();
		SqlBuilder.UPDATE("t_merchant_api_permission");
		if(StringUtil.isNotBlank(merchantApiPermission.getMerchantName())){
			SqlBuilder.SET("merchant_name=#{merchantName}");
		}
		if(StringUtil.isNotBlank(merchantApiPermission.getApiMethod())){
			SqlBuilder.SET("api_method=#{apiMethod}");
		}
		if(StringUtil.isNotBlank(merchantApiPermission.getIpWhiteList())){
			SqlBuilder.SET("ip_white_list=#{ipWhiteList}");
		}
		if(merchantApiPermission.getUpdateTime() != null){
			SqlBuilder.SET("update_time=#{updateTime}");
		}
		SqlBuilder.WHERE("id=#{id}");
		return SqlBuilder.SQL();
	}

}
